﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.User.Views;
using WCSFContrib.ECommerceCatalog.User.Tests.Mocks;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.User.Tests
{
    /// <summary>
    /// Summary description for DefaultPresenterTestFixture
    /// </summary>
    [TestClass]
    public class DefaultPresenterTestFixture
    {
        private MockDefaultView _view;
        private MockUserController _controller;

        public DefaultPresenterTestFixture()
        {
        }

        private DefaultPresenter GetDefaultPresenterInitialized()
        {
            _controller = new MockUserController();
            _view = new MockDefaultView();
            DefaultPresenter presenter = new DefaultPresenter(_controller);
            presenter.View = _view;

            return presenter;
        }

        [TestMethod]
        public void ShouldShowProfileOnViewInitializedWhenUserProfileExists()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.UserName = "userTest";
            presenter.OnViewInitialized();

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsFalse(_controller.CreateUserProfileCalled);
        }

        [TestMethod]
        public void ShouldShowProfileOnViewInitializedWhenUserProfileNotExists()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.UserName = "unknownUser";
            presenter.OnViewInitialized();

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.CreateUserProfileCalled);
        }

        [TestMethod]
        public void ShouldShowUserProfileOnViewLoadedWhenUserProfileExists()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.UserName = "userTest";
            presenter.OnViewLoaded();

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.GetOrdersByUserNameCalled);
            Assert.IsTrue(_view.ShowAddressListCalled);
            Assert.IsTrue(_view.TotalAddressCountCalled);
            Assert.IsTrue(_view.ShowPaymentsMethodsCalled);
            Assert.IsTrue(_view.TotalPaymentsCountCalled);
            Assert.IsTrue(_view.ShowOrdersCalled);
            Assert.IsTrue(_view.TotalOrdersCountCalled);
        }

        [TestMethod]
        public void ShouldShowUserProfileOnViewLoadedWhenUserProfileNotExists()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.UserName = "unknownUser";
            presenter.OnViewLoaded();

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.GetOrdersByUserNameCalled);
            Assert.IsFalse(_view.ShowAddressListCalled);
            Assert.IsFalse(_view.TotalAddressCountCalled);
            Assert.IsFalse(_view.ShowPaymentsMethodsCalled);
            Assert.IsFalse(_view.TotalPaymentsCountCalled);
            Assert.IsFalse(_view.ShowOrdersCalled);
            Assert.IsFalse(_view.TotalOrdersCountCalled);
        }

        [TestMethod]
        public void ShouldAddAddress()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            Address address = new Address();
            presenter.UserName = "userTest";
            presenter.OnAddAddress(address);

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.AddAddressCalled);
            Assert.IsTrue(_view.ShowAddressListCalled);
            Assert.IsTrue(_view.TotalAddressCountCalled);
        }

        [TestMethod]
        public void ShouldAddPaymentMethod()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            Payment payment = new Payment();
            presenter.UserName = "userTest";
            presenter.OnAddPaymentMethod(payment);

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.AddPaymentMethodCalled);
            Assert.IsTrue(_view.ShowPaymentsMethodsCalled);
            Assert.IsTrue(_view.TotalPaymentsCountCalled);
        }

        [TestMethod]
        public void ShouldDeleteAddress()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            Address address = new Address();
            presenter.UserName = "userTest";
            presenter.OnDeleteAddress(address);

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.RemoveAddressCalled);
        }

        [TestMethod]
        public void ShouldDeletePaymentMethod()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            Payment payment = new Payment();
            presenter.UserName = "userTest";
            presenter.OnDeletePaymentMethod(payment);

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.RemovePaymentMethodCalled);
        }

        [TestMethod]
        public void ShouldEditAddress()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            Address address = new Address();
            presenter.UserName = "userTest";
            presenter.OnEditAddress(address);

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.UpdateAddressCalled);
        }

        [TestMethod]
        public void ShouldEditPaymentMethod()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            Payment payment = new Payment();
            presenter.UserName = "userTest";
            presenter.OnEditPaymentMethod(payment);

            Assert.IsTrue(_controller.GetUserProfileCalled);
            Assert.IsTrue(_controller.UpdatePaymentCalled);
        }
    }

    class MockDefaultView : IDefaultView
    {
        public bool ShowAddressListCalled = false;
        public bool ShowPaymentsMethodsCalled = false;
        public bool ShowOrdersCalled = false;
        public bool TotalAddressCountCalled = false;
        public bool TotalPaymentsCountCalled = false;
        public bool TotalOrdersCountCalled = false;

        private int _totalAddressCount = 0;
        private int _totalPaymentsCount = 0;
        private int _totalOrdersCount = 0;

        #region IDefaultView Members

        public void ShowAddressList(ICollection<BusinessEntities.Address> addressList)
        {
            ShowAddressListCalled = true;
        }

        public void ShowPaymentsMethods(ICollection<BusinessEntities.Payment> PaymentsList)
        {
            ShowPaymentsMethodsCalled = true;
        }

        public void ShowOrders(ICollection<BusinessEntities.Order> orders)
        {
            ShowOrdersCalled = true;
        }

        public int TotalAddressCount
        {
            get
            {
                return _totalAddressCount;
            }
            set
            {
                TotalAddressCountCalled = true;
                _totalAddressCount = value;
            }
        }

        public int TotalPaymentsCount
        {
            get
            {
                return _totalPaymentsCount;
            }
            set
            {
                TotalPaymentsCountCalled = true;
                _totalPaymentsCount = value;
            }
        }

        public int TotalOrdersCount
        {
            get
            {
                return _totalOrdersCount;
            }
            set
            {
                TotalOrdersCountCalled = true;
                _totalOrdersCount = value;
            }
        }

        #endregion
    }
}

